package cn.fuxue.hrms.service.impl;

import cn.fuxue.hrms.common.base.AbstractService;
import cn.fuxue.hrms.common.entity.Permission;
import cn.fuxue.hrms.common.entity.Role;
import cn.fuxue.hrms.common.entity.User;
import cn.fuxue.hrms.mapper.PermissionMapper;
import cn.fuxue.hrms.mapper.RoleMapper;
import cn.fuxue.hrms.mapper.UserMapper;
import cn.fuxue.hrms.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl extends AbstractService<User,Integer> implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    @Override
    public void setBaseMapper() {
        super.setBaseMapper(userMapper);
    }


    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public List<User> findId(){
        return userMapper.findId();
    }

    @Override
    public User findById(Integer id) {
        User user=userMapper.findById(id);
        List<Role> roles=user.getRoles();
        for (int i=0;i<roles.size();i++){

            List<Permission> permissions=roleMapper.selectByPrimaryKey(roles.get(i).getRoleId()).getPermissions();
            for (int j=0;j<permissions.size();j++){
                Permission permission = permissionMapper.selectByPrimaryKey(permissions.get(j).getPermissionId());
                permissions.set(j,permission);
            }
            roles.get(i).setPermissions(permissions);
        }
        user.setRoles(roles);
        return user;
    }

    @Override
    public List<User> findCandidates() {
        return userMapper.findCandidates();
    }
}
